const { Controller } = require("egg");
class TopicController extends Controller {
  async list() {
    const { service } = this;
    try {
      const result = await service.topic.list();
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async teacherList() {
    const { service } = this;
    try {
      const result = await service.topic.teacherList();
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async lessonList() {
    const { service } = this;
    try {
      const result = await service.topic.lessonList();
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async materialList() {
    const { service } = this;
    try {
      const result = await service.topic.materialList();
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async create() {
    const { ctx, service } = this;
    const { title } = ctx.request.body;
    try {
      const result = await service.topic.create({
        title,
      });
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async delete() {
    const { ctx, service } = this;
    const { id } = ctx.request.body;
    try {
      const result = await service.topic.delete(id);
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async update() {
    const { ctx, service } = this;
    const { id, title } = ctx.request.body;
    if (!id || !title) {
      return this.ctx.helper.error("参数异常");
    }

    try {
      const result = await service.topic.update({
        id,
        title,
      });
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

}

module.exports = TopicController;
